package szwolaiyun.paireach.com.szwolaizhuang.net.json;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import szwolaiyun.paireach.com.szwolaizhuang.model.business.DriveOrder;
import szwolaiyun.paireach.com.szwolaizhuang.model.business.Goods;
import szwolaiyun.paireach.com.szwolaizhuang.model.business.Operator;
import szwolaiyun.paireach.com.szwolaizhuang.model.business.OperatorStatus;
import szwolaiyun.paireach.com.szwolaizhuang.model.business.User;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.AppEvent;

public class JsonPaser {

    private static final String TAG = "JsonPaser" ;

    /**
     * 解析用户登录返回信息
     *
     * @param json
     */
    public static void parseUserInfo(String json) {
        AppEvent event = new AppEvent();
        event.setTag("user_login_paser");
        try {
            JSONObject object = new JSONObject(json);
            String tmp = object.getString("loginResult");
            if (tmp.equals("0")) {
                //登录失败，没有该用户信息
                event.setCode(1);
                event.setExtraInfo("登录失败!");
            } else {
                //解析用户数据
                JSONObject object1 = object.getJSONObject("cpDriver");
                User user = new Gson().fromJson(object1.toString(), User.class);
                event.setCode(0);
                event.setObj1(user);
            }
        } catch (Exception e) {
            event.setCode(2);
            event.setExtraInfo("数据解析失败！");
        } finally {
            EventBus.getDefault().post(event);
        }
    }

    /**
     * 解析获取所有运单的信息
     *
     * @param json
     */
    public static void parseAllOrder(String json) {
        AppEvent event = new AppEvent();
        event.setTag("all_order_info");
        try {
            JSONObject object = new JSONObject(json);
            String tmp = object.getString("loadResult");
            if (tmp.equals("0")) {
                //失败，没有信息
                event.setCode(1);
                event.setExtraInfo("无运单");
            } else {
                //解析数据
                JSONArray object1 = object.getJSONArray("orders");
                List<DriveOrder> list = new Gson().fromJson(object1.toString(), new TypeToken<List<DriveOrder>>() {
                }.getType());
                event.setCode(0);
                event.setObj1(list);
            }
        } catch (Exception e) {
            event.setCode(2);
            event.setExtraInfo("数据解析失败！");
        } finally {
            EventBus.getDefault().post(event);
        }
    }

    /**
     * 解析首页返回的第一个未接单信息
     *
     * @param json
     */
    public static void parseFirstAvailableOrder(String json) {
        AppEvent event = new AppEvent();
        event.setTag("first_available_order_info");
        try {
            JSONObject object = new JSONObject(json);
            String tmp = object.getString("loadResult");
            if (tmp.equals("0")) {
                //登录失败，没有该用户信息
                event.setCode(1);
                event.setExtraInfo("无运单!");
            } else {
                //解析用户数据
                JSONObject object1 = object.getJSONObject("orders");
                DriveOrder user = new Gson().fromJson(object1.toString(), DriveOrder.class);
                event.setCode(0);
                event.setObj1(user);
            }
        } catch (Exception e) {
            event.setCode(2);
            event.setExtraInfo("数据解析失败！");
        } finally {
            EventBus.getDefault().post(event);
        }
    }


    /**
     * 司机接单
     *
     * @param result
     */
    public static void parseDriverAcceptOrder(String result) {
        AppEvent event = new AppEvent();
        event.setTag("driver_accept_order");
        if (result.equals("0")) {
            event.setCode(1);
            event.setExtraInfo("司机接单失败!");
        } else {
            event.setCode(0);
            event.setExtraInfo("司机接单成功！");
        }
        EventBus.getDefault().post(event);
    }

    /**
     * 解析获取运单中的商品信息
     *
     * @param json
     */
    public static void parseOrderDetailGoods(String json) {
        AppEvent event = new AppEvent();
        event.setTag("order_detail_goods");
        try {
            JSONObject object = new JSONObject(json);
            String tmp = object.getString("loadResult");
            if (tmp.equals("0")) {
                //失败，没有信息
                event.setCode(1);
                event.setExtraInfo("无商品");
            } else {
                //解析数据
                JSONArray object1 = object.getJSONArray("orders");
                List<Goods> list = new Gson().fromJson(object1.toString(), new TypeToken<List<Goods>>() {
                }.getType());
                event.setCode(0);
                event.setObj1(list);
            }
        } catch (Exception e) {
            event.setCode(2);
            event.setExtraInfo("数据解析失败！");
        } finally {
            EventBus.getDefault().post(event);
        }
    }

    /**
     * 签到信息
     *
     * @param json
     */
    public static void parseSignInfo(String json) {
        AppEvent event = new AppEvent();
        event.setTag("judge_sign_state");
        try {
            JSONObject object = new JSONObject(json);
            String tmp = object.getString("handlingresult");
            if (tmp.equals("0")) {
                //失败
                event.setCode(1);
//                event.setExtraInfo("签到失败");
                //提示“还有2公里，不可签到”
                event.setExtraInfo(object.getString("content"));
                Log.d(TAG,"setCode(1),content="+object.getString("content"));
            } else {
                event.setCode(0);
                event.setExtraInfo(object.optString("content"));
                Log.d(TAG,"setCode(0),content="+object.getString("content"));
            }
        } catch (Exception e) {
            event.setCode(2);
            event.setExtraInfo("数据解析失败！");
        } finally {
            EventBus.getDefault().post(event);
        }
    }
    /**
     * 签到信息
     *
     * @param json
     */
    public static void parseSignInfo2(String json) {
        AppEvent event = new AppEvent();
        event.setTag("judge_sign_state");
        try {
            JSONObject object = new JSONObject(json);
            String tmp = object.getString("handlingresult");
            if (!tmp.equals("1")){
                event.setCode(0);
                event.setExtraInfo("成功");
            }
        } catch (Exception e) {
            event.setCode(2);
            event.setExtraInfo("数据解析失败！");
        } finally {
            EventBus.getDefault().post(event);
        }
    }

    /**
     * 叉车工登录返回信息
     *
     * @param json
     */
    public static void parseOperatorInfo(String json) {
        AppEvent event = new AppEvent();
        event.setTag("operator_login_paser");
        try {
            JSONObject object = new JSONObject(json);
            String tmp = object.getString("loginResult");
            if (tmp.equals("0")) {
                //登录失败，没有该用户信息
                event.setCode(1);
                event.setExtraInfo("登录失败!");
            } else {
                //解析用户数据
                JSONObject object1 = object.getJSONObject("operator");
                Operator user = new Gson().fromJson(object1.toString(), Operator.class);
                event.setCode(0);
                event.setObj1(user);
            }
        } catch (Exception e) {
            event.setCode(2);
            event.setExtraInfo("数据解析失败！");
        } finally {
            EventBus.getDefault().post(event);
        }
    }

    /**
     * 获取运单详细信息
     *
     * @param json
     */
    public static void parseDriverOrderDetail(String json) {
        AppEvent event = new AppEvent();
        event.setTag("get_order_detail_info");
        try {
            JSONObject object = new JSONObject(json);
            String tmp = object.getString("result");
            if (tmp.equals("0")) {
                //失败
                event.setCode(1);
                event.setExtraInfo("获取运单失败!");
            } else {
                //解析
                JSONObject object1 = object.getJSONObject("order");
                DriveOrder user = new Gson().fromJson(object1.toString(), DriveOrder.class);
                event.setCode(0);
                event.setObj1(user);
            }
        } catch (Exception e) {
            event.setCode(2);
            event.setExtraInfo("数据解析失败！");
        } finally {
            EventBus.getDefault().post(event);
        }
    }

    public static void parseZhuangCheXieHuoState(String json) {
        AppEvent event = new AppEvent();
        event.setTag("zhuang_xie_state");
        try {
            JSONObject object = new JSONObject(json);
            event.setCode(0);
            event.setExtraInfo(object.optString("content"));
        } catch (Exception e) {
            event.setCode(2);
            event.setExtraInfo("数据解析失败！");
        } finally {
            EventBus.getDefault().post(event);
        }
    }

    /**
     * 获取运单详细，通过运单号
     * @param json
     */
    public static void parseDriverOrderDetailByOrderNumber(String json) {
        AppEvent event = new AppEvent();
        event.setTag("get_order_detail_info_by_no");
        try {
            JSONObject object = new JSONObject(json);
            String tmp = object.getString("queryResult");
            if (tmp.equals("0")) {
                //失败
                event.setCode(1);
                event.setExtraInfo("获取运单失败!");
            } else {
                //解析
                JSONObject object1 = object.getJSONObject("order");
                DriveOrder user = new Gson().fromJson(object1.toString(), DriveOrder.class);
                event.setCode(0);
                event.setObj1(user);
            }
        } catch (Exception e) {
            event.setCode(2);
            event.setExtraInfo("数据解析失败！");
        } finally {
            EventBus.getDefault().post(event);
        }
    }

    /**
     * DC进厂解析
     *
     * @param json
     */
    public static void parseDCEnterInfo(String json) {
//        Log.d(TAG,"parseDCEnterInfo---JSON="+json);
        AppEvent event = new AppEvent();
        event.setTag("judge_sign_state");
        try {
            JSONObject object = new JSONObject(json);
            String tmp = object.getString("handlingresult");
            if (tmp.equals("0")) {
                //失败
                event.setCode(1);
                event.setExtraInfo("失败");
            } else {
                event.setCode(0);
                event.setExtraInfo(object.optString("content"));
            }
        } catch (Exception e) {
            event.setCode(2);
            event.setExtraInfo("数据解析失败！");
        } finally {
            EventBus.getDefault().post(event);
        }
    }

    public static void 获取叉车工列表(String json){
        AppEvent event = new AppEvent();
        event.setTag("获取叉车工列表");
        try {
            JSONObject object = new JSONObject(json);
            String tmp = object.getString("queryResult");
            if (tmp.equals("0")) {
                //失败
                event.setCode(1);
                event.setExtraInfo("失败,未查询到其他人信息");
            } else {
                event.setCode(0);
                JSONArray object1=object.getJSONArray("operator");
                List<Operator> list = new Gson().fromJson(object1.toString(), new TypeToken<List<Operator>>() {
                }.getType());
                event.setObj1(list);
            }
        } catch (Exception e) {
            event.setCode(2);
            event.setExtraInfo("数据解析失败！");
        } finally {
            EventBus.getDefault().post(event);
        }
    }

    public static void 叉车工判断是否能进行装货卸货(String json){
        AppEvent event = new AppEvent();
        event.setTag("获取叉车工列表");
        try {
            JSONObject object = new JSONObject(json);
            String tmp = object.getString("loadType");
            if (tmp.equals("0")) {
                event.setCode(1);
            } else {
                event.setCode(0);
            }
            event.setExtraInfo(object.getString("content"));
        } catch (Exception e) {
            event.setCode(2);
            event.setExtraInfo("数据解析失败！");
        } finally {
            EventBus.getDefault().post(event);
        }
    }

    /**
     * 解析点击 “开始运输” 返回的数据
     * @param json
     */
    public static void parseStartTransport(String json) {
        AppEvent event = new AppEvent();
        event.setTag("parse_start_transport");
        try {
            JSONObject object = new JSONObject(json);
            boolean flag = object.getBoolean("");
            if (flag){
                event.setExtraInfo("签收");
            }
        } catch (JSONException e) {
            e.printStackTrace();
            event.setExtraInfo("解析数据失败");
        } finally {
            EventBus.getDefault().post(event);
        }
    }
}
